package cn.tedu.supermarket.mapper;

import cn.tedu.supermarket.entity.CartDetail;
import cn.tedu.supermarket.entity.CartVO;
import cn.tedu.supermarket.entity.GoodsDTO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CartMapper {

    @Insert("insert into cart values(null,#{goodsId},#{userId},#{num},#{sumPrice})")
    Integer goodsInfoCart(CartVO cartVO);
    @Select("select cart.id,cart.goodsId,goods.name,goods.imgurl,goods.price,cart.num,cart.sumPrice from goods right join cart on goods.id=cart.goodsId where userId=#{userId}")
    List<CartDetail> cartDetail(int id);
    @Select(" select * from cart where goodsId=#{goodsId} and userId=#{userId}")
    Integer selectByUserId(@Param("goodsId") int goodsId,@Param("userId") int userId);
    @Update("UPDATE cart SET num=num+1,sumPrice=sumprice+#{price} WHERE goodsId=#{goodsId} AND userId=#{userId}")
    Integer updateNum(@Param("price") int price,@Param("goodsId") int goodsId,@Param("userId") int userId);
    @Delete("delete from cart where id=#{id}")
    void deleteById(int id);
    @Select("select price from goods where id=#{id}")
    Integer selPriceById(int id);

    @Update("update cart set num=#{num1},sumPrice=#{num2}*#{price} where id=#{id}")
    Integer updateNumById(@Param("num1")int num1,@Param("num2")int num2,@Param("price")int price,@Param("id")int id);
    @Select("select price from goods left join cart on goods.id=cart.goodsId where cart.id=#{id}")
    Integer selectPriceByCartId(int id);
    @Select("select id from cart where userId=#{userId}")
    int[] selectNumByUserId(int userId);
    //提交订单后从购物车中删除
    @Delete("delete from cart where id=#{id}")
    Integer deleteGoodsById(int id);
    //查询goodsId与数量num
    @Select("select goodsId,num from cart where id=#{id}")
    GoodsDTO selectGoodsIdNumById(int id);
}
